<template>
	<view>
		<view class="search_title">
			<u-search class="search" @search="search" @custom="custom" :action-style="{color:'#fff'}" shape="square"
				placeholder="甜点" v-model="val">
			</u-search>
		</view>
		<view class="vip_title">
			<text>精品名厨视频-会员专享</text>
			<image src="/static/icons/diamond.png" mode=""></image>
		</view>
		<view class="member_menu">
			<view class="member_menu_item" v-for="item in vipRecommend" :key="item._id" @click="detail(item._id)">
				<image :src="item.coverpic" mode=""></image>
				<view>{{item.name}}</view>
				<view class="des">
					<view>
						<text>{{item.pageview}}</text>
						<image class="icon" src="/static/icons/watch.png" mode=""></image>
					</view>
					<view>
						<text>{{item.collections}}</text>
						<image class="icon" src="/static/icons/star.png" mode=""></image>
					</view>
				</view>
			</view>
		</view>
		<view class="search_list">
			<view class="search_item" v-for="item in searchData" :key="item._id" @click="detail(item._id)">
				<image :src="item.coverpic" mode=""></image>
				<view class="item_content">
					<text class="title">{{item.name}}</text>
					<view class="material">{{item.classtips.join("、")}}</view>
					<view class="visit">
						<view class="visit_item">
							<text>{{item.pageview}}</text>
							<image src="/static/icons/watch.png" mode=""></image>
						</view>
						<view class="visit_item">
							<text>{{item.collections}}</text>
							<image src="/static/icons/collection.png" mode=""></image>
						</view>
					</view>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				pageSize: 10,
				currentPage: 1,
				val: '',
				searchData: [],
				vipRecommend: [],
				total: 0,
				pages: 0,
			}
		},
		methods: {
			async getSearchVideo() {
				const result = await this.$api.home.getSearchVideo();
				if (result.meta.status == 200) {
					this.vipRecommend = result.message;
				}
			},
			async getSearchMenu() {
				const result = await this.$api.home.getSearchMenu({
					pageSize: this.pageSize,
					currentPage: this.currentPage,
					val: this.val
				})
				if (result.meta.status == 200) {
					this.total = result.total;
					this.pages = result.pages;
					this.searchData = result.menus;
				}
			},
			search() {
				this.getSearchMenu();
			},
			custom() {
				this.getSearchMenu();
			},
			// 页面跳转
			detail(id) {
				uni.navigateTo({
					url: `/pages/menuDetail/menuDetail?id=${id}`
				})
			},
			async onReachBottom() {
				if (this.currentPage < this.pages) {
					const result = await this.$api.home.getSearchMenu({
						pageSize: this.pageSize,
						currentPage: ++this.currentPage,
						val: this.val
					})
					this.searchData = [
						...this.searchData,
						...result.menus
					]
				}
			}
		},
		onLoad(options) {
			this.val = options.target;
			this.getSearchMenu(options.target);
		},
		onShow: function() {
			this.getSearchVideo();
		}
	}
</script>

<style lang="scss">
	.search_title {
		height: 100rpx;
		background: #DA7B36;
		padding: 10rpx 30rpx 10rpx 100rpx;
	}

	.vip_title {
		display: flex;
		align-items: center;
		margin: 30rpx 0 0 30rpx;

		>text {
			font-weight: 500;
		}

		>image {
			width: 40rpx;
			height: 40rpx;
		}
	}

	.member_menu {
		display: flex;
		overflow: auto;
		margin: 10rpx 20rpx;

		.member_menu_item {
			margin: 10rpx;

			>image {
				width: 320rpx;
				height: 220rpx;
				border-radius: 20rpx;
				overflow: hidden;
			}

			.des {
				display: flex;
				font-size: 24rpx;

				>view {
					display: flex;
					align-items: center;
					flex-grow: 1;
				}

				.icon {
					width: 40rpx;
					height: 40rpx;
					margin: 0 10rpx;
				}
			}
		}
	}

	.search_list {
		margin-top: 50rpx;
		padding: 0 20rpx;

		.search_item {
			display: flex;
			margin: 50rpx 0;

			>image {
				width: 300rpx;
				height: 200rpx;
				border-radius: 20rpx;
				overflow: hidden;
			}

			.item_content {
				margin-left: 20rpx;

				.title {
					display: block;
					margin: 20rpx 0;
				}

				.material {
					width: 320rpx;
					height: 40rpx;
					display: flex;
					color: #ccc;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap
				}

				.visit {
					margin-top: 30rpx;
					display: flex;
					align-items: center;
					color: #ccc;

					.visit_item {
						width: 140rpx;

						>image {
							width: 30rpx;
							height: 30rpx;
							margin-left: 20rpx;
						}
					}
				}
			}
		}
	}
</style>
